//
//  TMHomeOthersCell.m
//  国学商家管理
//
//  Created by mac on 2018/6/19.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMHomeOthersCell.h"
#import "TMRecommendModel.h"
@interface TMHomeOthersCell()<UIScrollViewDelegate>

@end
@implementation TMHomeOthersCell

- (void)setTypes:(NSArray *)types{
    _types = types;
    NSInteger size ;
    if (self.types.count%10 != 0) {
        size = (NSInteger)(self.types.count/10) + 1;
    }else{
        size = (NSInteger)(self.types.count/10);
    }
    self.pageControl.numberOfPages = size;
    [_itemSC setContentSize:CGSizeMake(kWindowW *size, 0)];
    //添加view
    NSArray *imageArray = @[@"初中同学",@"高中培训",@"国学培训",@"活动中心",@"精品课程",@"舞蹈",@"小学培训",@"初中同学",@"高中培训",@"国学培训",@"活动中心",@"精品课程",@"舞蹈",@"小学培训",@"小学培训",@"小学培训",@"小学培训",@"小学培训",@"小学培训",@"小学培训",@"小学培训"];
    [self layoutIfNeeded];
    NSInteger count = 1;
    UIView *Sview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW*2, self.itemSC.bounds.size.height)];
    for (int x = 0; x< size; x++) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                if (count-1 == self.types.count) {
                    return;
                }
                TMRecommendModel *model = self.types[count-1];
                NSLog(@"%ld",count);
                UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
                [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", imageArray[count-1]]] forState:UIControlStateNormal];
                btn.tag = count;
                [Sview addSubview:btn];
                NSLog(@"sssssssssss%f", (74*j+ 15)*kWindowW/375 +kWindowW *x);
//                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.left.equalTo(Sview).offset((74*j+ 15)*kWindowW/375+kWindowW*x);
//                    make.top.equalTo(Sview).offset((17+109*i)*kWindowW/375);
//                    make.height.mas_equalTo(@(50*kWindowW/375));
//                    make.width.mas_equalTo(@(50*kWindowW/375));
//                }];
               btn.frame = CGRectMake((74*j+ 15)*kWindowW/375 +kWindowW *x, (17+109*i)*kWindowW/375, 50*kWindowW/375, 50*kWindowW/375);
                count++;
                UILabel *label = [[UILabel alloc]init];
                [Sview addSubview:label];
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(btn);
                    make.top.equalTo(btn.mas_bottom).offset(13*kWindowW/375);
                }];
                label.font = [UIFont systemFontOfSize:14];
                label.text = [NSString stringWithFormat:@"%@", model.name];
                label.textColor = [UIColor hx_colorWithHexRGBAString:@"585858"];
            }
        }
        [_itemSC addSubview:Sview];
    }
    
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = [UIColor whiteColor];
    //隐藏滑动条
    _itemSC.showsHorizontalScrollIndicator = NO;
    _itemSC.showsVerticalScrollIndicator = NO;
    //开启翻页模式
    _itemSC.pagingEnabled = YES;
    //关闭弹簧效果
    _itemSC.bounces = NO;
    
    //设置代理用于改变page
    _itemSC.delegate = self;
}
- (void)btnClick:(UIButton *)sender{
    if (self.block) {
        self.block(sender.tag-1);
    }
}

#pragma mark -------   scrollView代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //获取当前是第几页
    int page = _itemSC.contentOffset.x/kWindowW;
    
    //给page赋值
    _pageControl.currentPage = page;
}
- (void)displayBackSC{
    
}
- (IBAction)pageControlAction:(UIPageControl *)sender {
     int page = _itemSC.contentOffset.x/kWindowW;
    if (sender.currentPage != page) {
        [_itemSC setContentOffset:CGPointMake(kWindowW *sender.currentPage, 0)];
    }
}

@end
